package org.hxyjs.service.impl;


import org.hxyjs.dao.ChildrenDao;
import org.hxyjs.dto.DTO;
import org.hxyjs.dto.MessageEnum;
import org.hxyjs.entity.Children;
import org.hxyjs.entity.Children;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.ChildrenService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service("ChildrenService")
public class ChildrenServiceImpl implements ChildrenService {
    @Resource
    ChildrenDao childrenDao;
    @Override
    public DTO<Children> getAllChildrens()  throws Exception{
        List<Children> childrens = childrenDao.getAllChildrens();
        MessageEnum messageEnum  = Optional.ofNullable(childrens).isPresent()?MessageEnum.GETCHILDRENSUCCESS: MessageEnum.GETCHILDRENFAIL;
        return new DTO<Children>(messageEnum.getCode(),messageEnum.getMessage(),childrens);
    }

    @Override
    public DTO<Children> deleteChildrenByCno(String cno)  throws Exception{
        Integer result = childrenDao.deleteChildrenByCno(cno);
        MessageEnum messageEnum  =result> 0?MessageEnum.DELETECHILDRENSUCCESS:MessageEnum.DELETECHILDRENFAIL;
        return new DTO<Children>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<Children> updateChildrenByCno(Children children)  throws Exception{
        Integer result = childrenDao.updateChildrenByCno(children);
        MessageEnum messageEnum  =result> 0?MessageEnum.UPDATECHILDRENSUCCESS:MessageEnum.UPDATECHILDRENFAIL;
        return new DTO<Children>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<Children> addChildren(Children children) throws Exception {
        Integer result = childrenDao.addChildren(children);
        MessageEnum messageEnum  = result> 0?MessageEnum.ADDCHILDRENSUCCESS:MessageEnum.ADDCHILDRENFAIL;
        return new DTO<Children>(messageEnum.getCode(),messageEnum.getMessage());
    }
    @Override
    public DTO<Children> getChildrenByPage(PageInfo pageInfo ) throws Exception {
        List<Children> childrens = childrenDao.getChildrenByPage(pageInfo);
        MessageEnum messageEnum  = Optional.ofNullable(childrens).isPresent()?MessageEnum.GETCHILDRENSUCCESS: MessageEnum.GETCHILDRENFAIL;
        return new DTO<Children>(messageEnum.getCode(),messageEnum.getMessage(),childrens,pageInfo);
    }
}
